//
//  UserEditeImageViewController.m
//  WULIHH
//
//  Created by tang on 16/5/3.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "UserEditeImageViewController.h"
#import "Header.h"

#import "TitleTableView.h"
#import "LoadShowEditeImageView.h"
#import "ColorChangeView.h"
#import "QiPaoView.h"
#import "ImageTextFieldView.h"

#import "PostUploadImageDataBase.h"


#import "CropperImageViewController.h"
#import "PostUpLoadImageViewController.h"
#import "ThreeViewController.h"


#define buttonPX            WithView/3.0


@interface UserEditeImageViewController ()<TitleTableViewDelegate,LoadShowEditeImageViewDelegate, QiPaoViewDelegate,CropperImageViewControllerDelegate, ImageTextFieldViewDelegate, ColorChangeViewDelegate>
{
    TitleTableView *titleTableView;
    LoadShowEditeImageView *loadImageView;
    QiPaoView               *qipaoView;
    
    UIButton                *biliButton;
    UIButton                *kuangButton;
    UIButton                *colorButton;
    
    
    UIButton                *cancleButton;
    ImageTextFieldView      *textView;

    
    
    int                      currentInt;
    
    NSDictionary            *lastQipaoDic;//上一张图片的气泡信息
}


@property (nonatomic, strong) ColorChangeView *changeColorView;
@property (nonatomic, strong) NSMutableArray *totalMutableArray;


@end

@implementation UserEditeImageViewController



- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _totalMutableArray = [NSMutableArray new];
    
    cancleButton = [[UIButton alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:cancleButton];
    [cancleButton setHidden:YES];
    
    
    titleTableView = [[TitleTableView alloc] initWithFrame:CGRectMake(0, 0, WithView, 60.0)];
    [titleTableView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:titleTableView];
    
    loadImageView = [[LoadShowEditeImageView alloc] init];
    [self.view addSubview:loadImageView];
    
    
    biliButton = [[UIButton alloc] initWithFrame:CGRectMake(0, heightView-46, buttonPX, 46)];
    [self.view addSubview:biliButton];
    
    kuangButton = [[UIButton alloc] initWithFrame:CGRectMake(buttonPX, heightView-46, buttonPX, 46)];
    [self.view addSubview:kuangButton];
    
    colorButton = [[UIButton alloc] initWithFrame:CGRectMake(2*buttonPX, heightView-46, buttonPX, 46)];
    [self.view addSubview:colorButton];

    
    _changeColorView = [[ColorChangeView alloc] initWithFrame:CGRectMake(0, heightView-46-93, WithView, 93.0)];
    [_changeColorView setBackgroundColor:uicolor(237, 237, 237)];
    [self.view addSubview:_changeColorView];
    [_changeColorView setHidden:YES];


    qipaoView = [[QiPaoView alloc] initWithFrame:CGRectMake(0, heightView-46-93, WithView, 93.0)];
    [qipaoView setBackgroundColor:uicolor(249, 249, 249)];
    [self.view addSubview:qipaoView];

    
    textView = [[ImageTextFieldView alloc] initWithFrame:CGRectMake(0, heightView - 48.0, WithView, 48.0)];
    
    [self.view addSubview:textView];
    [textView setHidden:YES];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [_totalMutableArray addObjectsFromArray:_imageMutableArray];
    
    [textView setMImageTextFieldViewDelegate:self];
    [_changeColorView setMColorChangeViewDelegate:self];
    [titleTableView setMTitleTableViewDelegate:self];
    [titleTableView setDataArray:_totalMutableArray];
    
    [loadImageView setMLoadShowEditeImageViewDelegate:self];
    
    [biliButton.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
    [biliButton setBackgroundColor:uicolor(249, 249, 249)];
    [biliButton setTitle:@"照片比例" forState:UIControlStateNormal];
    [biliButton setTitleColor:uicolor(117, 117, 117) forState:UIControlStateNormal];
    
    UIView *biliView = [[UIView alloc] initWithFrame:CGRectMake(biliButton.bounds.size.width - 0.5, 15, 1, 17)];
    [biliView setBackgroundColor:uicolor(205, 205, 205)];
    [biliButton addSubview:biliView];
    
    [kuangButton setSelected:YES];
    [kuangButton.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
    [kuangButton setBackgroundColor:uicolor(249, 249, 249)];
    [kuangButton setTitle:@"气泡" forState:UIControlStateNormal];
    [kuangButton setTitleColor:uicolor(117, 117, 117) forState:UIControlStateNormal];
    
    UIView *kuangView = [[UIView alloc] initWithFrame:CGRectMake(kuangButton.bounds.size.width - 1, 15, 1, 17)];
    [kuangView setBackgroundColor:uicolor(205, 205, 205)];
    [kuangButton addSubview:kuangView];
    
    [colorButton.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
    [colorButton setBackgroundColor:uicolor(249, 249, 249)];
    [colorButton setTitle:@"文本样式" forState:UIControlStateNormal];
    [colorButton setTitleColor:uicolor(117, 117, 117) forState:UIControlStateNormal];
    [colorButton setSelected:NO];
    
    [biliButton setTag:1];
    [kuangButton setTag:2];
    [colorButton setTag:3];
    
    
    [biliButton addTarget:self action:@selector(changeEdite:) forControlEvents:UIControlEventTouchUpInside];
    [kuangButton addTarget:self action:@selector(changeEdite:) forControlEvents:UIControlEventTouchUpInside];
    [colorButton addTarget:self action:@selector(changeEdite:) forControlEvents:UIControlEventTouchUpInside];
    
    [cancleButton addTarget:self action:@selector(removeKeyBoard) forControlEvents:UIControlEventTouchUpInside];
    
    [qipaoView setMQiPaoViewDelegate:self];

    
    currentInt = 0;
    [[PostUploadImageDataBase sharedInstance] readImageWithSelect:[NSString stringWithFormat:@"%d", currentInt] wirhInfo:^(NSData *imageData, NSDictionary *qipaoInfoDic){
    
        [loadImageView setImage:[UIImage imageWithData:imageData]];
    }];
    
    [titleTableView setSelectInt:0];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qipaoChangeWord:) name:@"chuxianjianpan" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addPhotoGetBack:) name:@"addphoto" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark----返回
- (void)popToView
{
//    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark---添加图片
- (void)addPhotoGetBack:(NSNotification *)notion
{
    [_totalMutableArray addObjectsFromArray:[notion object]];
//    NSLog(@"---------%d", _totalMutableArray.count);
    [titleTableView setDataArray:_totalMutableArray];
}




#pragma mark---编辑完成
- (void)wanchengBuoont
{
    //获取当前截图
    UIImage *image = [loadImageView cropImage];
    NSUInteger i = UIImageJPEGRepresentation(image, 1.0).length/1024.0;
    
    NSData *imageData = nil;
    if (UIImageJPEGRepresentation(image, 1.0).length/1024.0 > 190)
    {
        imageData = UIImageJPEGRepresentation(image, 190.0/i);
    }
    else
    {
        imageData = UIImageJPEGRepresentation(image, 1.0);
    }
    
    [[PostUploadImageDataBase sharedInstance] readCurrentImageWithSelect:[NSString stringWithFormat:@"%d", currentInt] withLastString:[NSString stringWithFormat:@"%d", currentInt] withLastIMage:imageData withLastQiapo:lastQipaoDic withCallBack:^(NSData *imageData, NSDictionary *infoDic){
        
        PostUpLoadImageViewController *postImageVC = [[PostUpLoadImageViewController alloc] init];
        postImageVC.totalInt = (int)_totalMutableArray.count;
        [self.navigationController pushViewController:postImageVC animated:YES];

    }];

}



#pragma mark----添加图片
- (void)tianjiaImage
{
    ThreeViewController *threeView = [[ThreeViewController alloc] init];
    threeView.plusImage = YES;
    [self presentViewController:threeView animated:YES completion:nil];
}





#pragma mark---设置feame
- (void)withImageFrame:(CGSize)size
{
    [loadImageView setFrame:CGRectMake(0, 60, size.width, size.height)];
    [loadImageView setCenter:CGPointMake(WithView/2.0, loadImageView.center.y)];
}



- (void)chooseCurrentWithName:(NSString *)currentName lastWithName:(NSString *)lastName
{
    currentInt = [currentName intValue];
//    NSLog(@"%@ ", currentName);
//获取当前截图
    UIImage *image = [loadImageView cropImage];
    NSUInteger i = UIImageJPEGRepresentation(image, 1.0).length/1024.0;
    
    NSData *imageData = nil;
    if (UIImageJPEGRepresentation(image, 1.0).length/1024.0 > 190)
    {
        imageData = UIImageJPEGRepresentation(image, 190.0/i);
    }
    else
    {
        imageData = UIImageJPEGRepresentation(image, 1.0);
    }
    
    [[PostUploadImageDataBase sharedInstance] readCurrentImageWithSelect:currentName withLastString:lastName withLastIMage:imageData withLastQiapo:lastQipaoDic withCallBack:^(NSData *imageData, NSDictionary *infoDic){
    
        [loadImageView loadTextQipao:infoDic];
//        NSLog(@"%@", [UIImage imageWithData:imageData]);
        lastQipaoDic = infoDic;
        [loadImageView setImage:[UIImage imageWithData:imageData]];
    }];
}



//编辑照片
- (void)changeEdite:(UIButton *)sender
{
    //    [self buttonReload];
    if (sender.tag == 1)
    {
        CropperImageViewController *cropView = [[CropperImageViewController alloc] init];
        cropView.sourceImage = [UIImage imageWithData:_totalMutableArray[currentInt] ];
        [cropView setMCropperImageViewControllerDelegate:self];
        [self presentViewController:cropView animated:YES completion:nil];
        
        [biliButton setTitleColor:uicolor(36, 36, 36) forState:UIControlStateNormal];
        [kuangButton setTitleColor:uicolor(117, 117, 117) forState:UIControlStateNormal];
        [colorButton setTitleColor:uicolor(117, 117, 117) forState:UIControlStateNormal];
    }
    else if (sender.tag == 2)
    {
        //        [kuangButton        setBackgroundColor:uicolor(35, 35, 35)];
        //        [_qipaoView         setHidden:NO];
        if ([kuangButton isSelected]) {
            
            [kuangButton setSelected:NO];
            [qipaoView setHidden:YES];
        }
        else
        {
            [kuangButton setSelected:YES];
            [qipaoView setHidden:NO];
            
        }
        [kuangButton setTitleColor:uicolor(36, 36, 36) forState:UIControlStateNormal];
        [biliButton setTitleColor:uicolor(117, 117, 117) forState:UIControlStateNormal];
        [colorButton setTitleColor:uicolor(117, 117, 117) forState:UIControlStateNormal];
        
    }
    else if (sender.tag == 3)
    {
        [kuangButton setSelected:NO];
        [qipaoView setHidden:YES];
        
        if ([colorButton isSelected]) {
            
            [colorButton setSelected:NO];
            [_changeColorView setHidden:YES];
        }
        else
        {
            [colorButton setSelected:YES];
            [_changeColorView setHidden:NO];
            
        }
        
        [kuangButton setTitleColor:uicolor(117, 117, 117) forState:UIControlStateNormal];
        [biliButton setTitleColor:uicolor(117, 117, 117) forState:UIControlStateNormal];
        [colorButton setTitleColor:uicolor(36, 36, 36) forState:UIControlStateNormal];
        
    }
}





#pragma mark---裁剪图片保存
- (void)croperWithImage:(UIImage *)image withRect:(CGRect)rectSize
{
    [loadImageView setImage:image];
}




#pragma mark----选择气泡
- (void)getQipaoImageNameInfo:(NSDictionary *)infoDic
{
    lastQipaoDic = infoDic;
    [loadImageView loadTextQipao:infoDic];
}



- (void)setQipaoDic:(NSDictionary *)dic
{
//    NSLog(@"%@", dic);
    lastQipaoDic = dic;
}





#pragma mark----弹出键盘

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    //键盘高度
    CGRect keyBoardFrame = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [textView setFrame:CGRectMake(0, heightView-keyBoardFrame.size.height-48, WithView, 48.0)];
}


- (void)keyboardWillBeHidden:(NSNotification *)notion
{
    [textView setHidden:YES];
    [textView.textfiled setText:@""];
    [textView setFrame:CGRectZero];
}

- (void)qipaoChangeWord:(NSNotification *)notion
{
    [cancleButton setHidden:NO];
    [textView.textfiled becomeFirstResponder];
    [textView setHidden:NO];

}


- (void)removeKeyBoard
{
    [cancleButton setHidden:YES];
    [textView.textfiled setText:@""];
    [textView.textfiled resignFirstResponder];
}



#pragma mark---改变文字，颜色，透明度
- (void)changeText:(NSString *)contentString
{
//    NSLog(@"%@", contentString);
    [loadImageView setContentString:contentString];
}


- (void)changeAlpha:(float)alpha
{
    [loadImageView setAlphaValue:alpha];
}

- (void)changeTextColor:(NSString *)colorString
{
    [loadImageView setChangeTextColor:colorString];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
